<?php

namespace App\Console\Commands;

use App\Library\Logger;
use App\Models\ConfigDataModel;
use App\Models\ConfigModel;
use Illuminate\Console\Command;

class SetApp extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'set:app';

    private $logger;

    private $configCode = 'app';

    private $androidVersion = 'android_version';
    private $androidUrl = 'android_url';
    private $androidDescription = 'android_description';

    private $iosVersion = 'ios_version';
    private $iosUrl = 'ios_url';
    private $iosDescription = 'ios_description';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'app(Android、iOS版本)配置';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
        $this->logger = new Logger('set/app');
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $bench = new \Ubench();
        $bench->start();

        $this->action();

        $bench->end();
        $this->logger->debug("time used: " . $bench->getTime());
        $this->logger->debug("memory used: " . $bench->getMemoryUsage());
    }

    private function action()
    {
        $this->addBaseConfig();
        $this->addConfigItems();
    }

    private function addBaseConfig()
    {
        $this->logger->info("添加配置项...");
        $appConfigCount = \App\Models\ConfigModel::query()->where('code', $this->configCode)->count();
        if ($appConfigCount) {
            $this->logger->info("配置项已存在， 跳过...");
        }
    }

    private function addConfigItems()
    {
        $this->logger->info('添加配置项明细...');
        $configData = ConfigModel::query()->where('code', $this->configCode)->first();
        $configId = $configData->id;

        /**
         * 需添加的数据
         * android_version: 安卓最新版本
         * android_url: 安卓下载地址
         * android_description: 安卓说明
         *
         * ios_version: iOS最新版本
         * ios_url: iOS下载地址
         * ios_description: iOS说明
         */

        $configDataItems = [];

        $androidVersionCount = ConfigDataModel::query()->where('code', $this->androidVersion)->count();
        if (!$androidVersionCount) {
            $configDataItems[] = [
                'title' => 'Android最新版本',
                'note' => '',
                'options' => '',
                'code' => $this->androidVersion,
                'value' => '',
                'config_id' => $configId,
                'type' => 'number',
                'status' => 1,
                'sort' => 1,
                'create_user' => 1,
                'update_user' => 1,
                'mark' => 1
            ];
        }
        $androidUrlCount = ConfigDataModel::query()->where('code', $this->androidUrl)->count();
        if (!$androidUrlCount) {
            $configDataItems[] = [
                'title' => 'Android下载地址',
                'note' => '',
                'options' => '',
                'code' => $this->androidUrl,
                'value' => '',
                'config_id' => $configId,
                'type' => 'text',
                'status' => 1,
                'sort' => 2,
                'create_user' => 1,
                'update_user' => 1,
                'mark' => 1
            ];
        }
        $androidDescriptionCount = ConfigDataModel::query()->where('code', $this->androidDescription)->count();
        if (!$androidDescriptionCount) {
            $configDataItems[] = [
                'title' => 'Android版本说明',
                'note' => '',
                'options' => '',
                'code' => $this->androidDescription,
                'value' => '',
                'config_id' => $configId,
                'type' => 'textarea',
                'status' => 1,
                'sort' => 3,
                'create_user' => 1,
                'update_user' => 1,
                'mark' => 1
            ];
        }

        $iosVersionCount = ConfigDataModel::query()->where('code', $this->iosVersion)->count();
        if (!$iosVersionCount) {
            $configDataItems[] = [
                'title' => 'iOS最新版本',
                'note' => '',
                'options' => '',
                'code' => $this->iosVersion,
                'value' => '',
                'config_id' => $configId,
                'type' => 'number',
                'status' => 1,
                'sort' => 4,
                'create_user' => 1,
                'update_user' => 1,
                'mark' => 1
            ];
        }
        $iosUrlCount = ConfigDataModel::query()->where('code', $this->iosUrl)->count();
        if (!$iosUrlCount) {
            $configDataItems[] = [
                'title' => 'iOS下载地址',
                'note' => '',
                'options' => '',
                'code' => $this->iosUrl,
                'value' => '',
                'config_id' => $configId,
                'type' => 'text',
                'status' => 1,
                'sort' => 5,
                'create_user' => 1,
                'update_user' => 1,
                'mark' => 1
            ];
        }
        $iosDescriptionCount = ConfigDataModel::query()->where('code', $this->iosDescription)->count();
        if (!$iosDescriptionCount) {
            $configDataItems[] = [
                'title' => 'iOS版本说明',
                'note' => '',
                'options' => '',
                'code' => $this->iosDescription,
                'value' => '',
                'config_id' => $configId,
                'type' => 'textarea',
                'status' => 1,
                'sort' => 6,
                'create_user' => 1,
                'update_user' => 1,
                'mark' => 1
            ];
        }

        $this->logger->info("添加配置项: " . count($configDataItems));

        foreach ($configDataItems as $index => $item) {
            $key = $index + 1;
            $this->logger->info("添加第{$key}项数据");
            $configDataModel = new ConfigDataModel();
            $configDataModel->fill($item);
            $configDataModel->saveOrFail();
        }
    }
}
